home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13135 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  60 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Forward declarations: why won't they work?
  5. Date: 23 Mar 1996 22:00:48 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4j1sag$qnt@arl-news-svc-3.compuserve.com>
  8. NNTP-Posting-Host: ad53-232.compuserve.com
  9.  
  10. gusty@clark.net (Harlan Messinger) s'Θcrit :
  11. > Theodore Sternberg (strnbrg@rahul.net) wrote:
  12. > : Forward class declarations only seem to work sometimes.  When they don't 
  13. > : work, I get a compiler error to the effect that "struct foo is an 
  14. > : imcomplete type".  Can anyone tell me what's going on, i.e. when forward 
  15. > : class declarations are and are not possible?
  16. > : 
  17. > You'll get an error when the compiler would have to know, not just that 
  18. > your struct is a struct, but what's IN the struct, in order to compile 
  19. > your code.
  20. > For example,
  21. >     struct Foo;
  22. >     struct Bar
  23. >     {
  24. >         Foo *f;
  25. >     };
  26. > is fine. The compiler knows that Foo is a struct, and that f is therefore 
  27. > a pointer-to-struct. The way a compiler sets aside space for a 
  28. > pointer-to-struct is independent of the contents of the struct, so the 
  29. > compiler is able to deal with the definition of Bar without having to 
  30. > know what a Foo looks like. But,
  31. >     struct Foo;
  32. >     struct Bar
  33. >     {
  34. >         Foo f;
  35. >     };
  36. > won't work because now you're telling the compiler that a Foo itself, not 
  37. > a pointer to it, is a member of Bar, and you are asking the computer to 
  38. > set aside space for a Foo itself. To do this, the compiler _does_ have to 
  39. > know what a Foo looks like. Since you haven't told it yet, you will get an 
  40. > error.
  41.  
  42. I should add also the following code which does not work
  43. either:
  44.   struct Foo:
  45.   struct Bar {
  46.      Foo *f;
  47.   }
  48.   
  49.   int FooBar(Bar *b) {
  50.     (b->f)++; // error here
  51.   }
  52. because b->f is a pointer to an unknown structure, which then
  53. has no arithmetic defined on it (the size of Foo is unknown).
  54.